﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.DAO;
using log4net;

namespace MMS
{

    public partial class UserControlUnitList : UserControl
    {
        UnitDAO unitDAO = new UnitDAO(Param.ConnectionString.Value);
        public UserControlUnitList()
        {
            InitializeComponent();
        }

        private void UserControlUnitList_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
        }

        private void gridControlUnit_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            gridViewUnit.OptionsBehavior.AutoPopulateColumns = false;
            PopulateData();
        }

        private void PopulateData()
        {
            List<UnitDTO> unitList = new List<UnitDTO>();
            unitList = unitDAO.SelectAll();
            gridControlUnit.DataSource = unitList;

            Id.FieldName = "Id";
            UnitName.FieldName = "UnitName";
            Note.FieldName = "Note";

        }

        private UnitDTO getSelectedUnit()
        {
            UnitDTO selectedUnit = null;

            string unitId = gridViewUnit.GetFocusedRowCellValue(Id).ToString();
            selectedUnit = unitDAO.Select(unitId);
            return selectedUnit;
        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string tsbName = e.Item.Name;
            if (tsbName.Equals(bbiAdd.Name))
            {
                using (FormUnitInfo f = new FormUnitInfo())
                {
                    f.Text = MessageBundle.ADD_NEW_UNIT.Value;
                    f.ShowDialog();
                    PopulateData();
                }
            }
            else if (tsbName.Equals(bbiEdit.Name))
            {
                UnitDTO selectedUnit = getSelectedUnit();
                using (FormUnitInfo f = new FormUnitInfo(selectedUnit))
                {
                    f.Text = MessageBundle.EDIT_UNIT.Value;
                    f.ShowDialog();
                    PopulateData();
                }

            }
            else if (tsbName.Equals(bbiDelete.Name))
            {
                UnitDTO selectedUnit = getSelectedUnit();

                try
                {
                    DialogResult deleteResult = Utils.ShowAuthentication(MessageBundle.DELETE_WARNING_CONTENT.Value);
                    if (deleteResult == DialogResult.Yes)
                    {
                        if (ValidateUtil.canDelete(selectedUnit))
                        {

                            unitDAO.Delete(selectedUnit.Id);
                            LogUtil.log.Error("You have just deleted this Unit" + selectedUnit.Id);
                        }
                        else
                        {
                            Utils.ShowError(MessageBundle.CANNOT_DELETE_CONTENT.Value);
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    Utils.ShowError(MessageBundle.CANNOT_DELETE_CONTENT.Value);
                    LogUtil.log.Error("can not delete this unit" + selectedUnit.Id);
                    LogUtil.log.Error(ex);
                }

                PopulateData();
            }

            else if (tsbName.Equals(bbiExit.Name))
            {

            }
        }



    }
}
